[TSAN] Rework assertions to always access refcount atomically
authorColin Walters <walters@verbum.org>
Thu, 17 Nov 2016 16:40:59 +0000 (11:40 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 17 Nov 2016 19:41:57 +0000 (19:41 +0000)
`-fsanitize=address` complained that the `refcount > 0` assertions
were reading without atomics.  We can fix this by reworking them
to read the previous value.

Closes: #582
Approved by: jlebon

src/libostree/ostree-fetcher.c
src/libostree/ostree-repo-commit.c
src/libostree/ostree-repo.c

index 87e084418aecfb3719b8e336a4444327c84e1fbd..c9161d409cf07dbdaecd6e23e4ca67907deb3cd2 100644 (file)
@@ -128,11 +128,10 @@ G_DEFINE_TYPE (OstreeFetcher, _ostree_fetcher, G_TYPE_OBJECT)
 static ThreadClosure *
 thread_closure_ref (ThreadClosure *thread_closure)
 {
+  int refcount;
   g_return_val_if_fail (thread_closure != NULL, NULL);
-  g_return_val_if_fail (thread_closure->ref_count > 0, NULL);
-
-  g_atomic_int_inc (&thread_closure->ref_count);
-
+  refcount = g_atomic_int_add (&thread_closure->ref_count, 1);
+  g_assert (refcount > 0);
   return thread_closure;
 }
 
@@ -140,7 +139,6 @@ static void
 thread_closure_unref (ThreadClosure *thread_closure)
 {
   g_return_if_fail (thread_closure != NULL);
-  g_return_if_fail (thread_closure->ref_count > 0);
 
   if (g_atomic_int_dec_and_test (&thread_closure->ref_count))
     {
@@ -197,11 +195,10 @@ pending_task_compare (gconstpointer a,
 static OstreeFetcherPendingURI *
 pending_uri_ref (OstreeFetcherPendingURI *pending)
 {
+  gint refcount;
   g_return_val_if_fail (pending != NULL, NULL);
-  g_return_val_if_fail (pending->ref_count > 0, NULL);
-
-  g_atomic_int_inc (&pending->ref_count);
-
+  refcount = g_atomic_int_add (&pending->ref_count, 1);
+  g_assert (refcount > 0);
   return pending;
 }
 
index 84d1537417ce13dc743ba690e00d72dc9a7ef753..eb9733e8f8ed8e5bd2bedb46f329061b9ce229a3 100644 (file)
@@ -2959,7 +2959,8 @@ ostree_repo_commit_modifier_new (OstreeRepoCommitModifierFlags  flags,
 OstreeRepoCommitModifier *
 ostree_repo_commit_modifier_ref (OstreeRepoCommitModifier *modifier)
 {
-  g_atomic_int_inc (&modifier->refcount);
+  gint refcount = g_atomic_int_add (&modifier->refcount, 1);
+  g_assert (refcount > 0);
   return modifier;
 }
 
index d3762521990c18bebe0e2ed28c395639764e362b..b4cf4e38be88556e644f84fdd1da93094b709a35 100644 (file)
@@ -161,11 +161,10 @@ ost_remote_new_from_keyfile (GKeyFile    *keyfile,
 static OstreeRemote *
 ost_remote_ref (OstreeRemote *remote)
 {
+  gint refcount;
   g_return_val_if_fail (remote != NULL, NULL);
-  g_return_val_if_fail (remote->ref_count > 0, NULL);
-
-  g_atomic_int_inc (&remote->ref_count);
-
+  refcount = g_atomic_int_add (&remote->ref_count, 1);
+  g_assert (refcount > 0);
   return remote;
 }